﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using DatabaseAdminTools.Connections;

namespace DatabaseAdminTools.MiniForms
{
    public partial class ConnectionForm : Form
    {
        private Connection conn;
        private String connName;
        private String host;
        private int port;
        private String user;
        private String pass;
        private bool isNewConnection;
        private TreeNode connectionNode;

        public ConnectionForm()
        {
            InitializeComponent();
            conn = new Connection();
            isNewConnection = true;
        }

        public ConnectionForm(TreeNode selectedNode,  Connection initConnection)
        {
            InitializeComponent();
            conn = initConnection;
            connectionNode = selectedNode;
            setData();
            isNewConnection = false;
        }

        private void setData()
        {
            connName = connTextBox.Text = conn.Name;
            host = hostTextBox.Text = conn.hostName;
            portTextBox.Text = conn.port.ToString();
            port = conn.port;
            user = userTextBox.Text = conn.userName;
            pass = passTextBox.Text = conn.pass;
        }

        private void setVariables()
        {
            connName = connTextBox.Text;
            host = hostTextBox.Text;
            port = int.Parse(portTextBox.Text);
            user = userTextBox.Text;
            pass = passTextBox.Text;
            if (connName.Equals(""))
            {
                connName = host;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (testConnection())
            {
                MessageBox.Show(this, "Connection successful !");
            }
        }

        private bool testConnection()
        {
            setVariables();
            WaitDialog waitDialog = new WaitDialog("Connecting to " + host + "...");
            Thread t = new Thread(waitDialog.show);
            t.Start();
            try
            {
                conn.setConnection(connName, host, user, pass, port);
            }
            catch (Exception ex)
            {
                t.Abort();
                System.Windows.Forms.MessageBox.Show(null, ex.Message, "Error",
                    System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                return false;
            }
            t.Abort();
            return true;
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            if (testConnection())
            {
                if (isNewConnection)
                {
                    ((Form1)this.Owner).addConnection(false, conn);
                }
                else
                {
                    ((Form1)this.Owner).updateConnectionProperties(connectionNode, conn);
                }
                this.Dispose();
            }
        }

        private void ConnectionForm_Load(object sender, EventArgs e)
        {

        }
    }
}
